﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using QuanLyBanHang.DataConnection;

namespace QuanLyBanHang.QuerySQL
{
    class ClassqlNhaphang
    {
        Connect conn = new Connect();
        public DataTable ShowQLPN(int i, string pn = null, string ncc = null,string ctpn = null)
        {
            DataTable tb = new DataTable(); ;
            switch (i)
            {
                case 0:
                    {
                        try
                        {
                            string sql = "select MAPN,TENNCC,TENNV,NGAYLAP,TONGTIEN from PHIEUNHAP,NHACC,NHANVIEN where PHIEUNHAP.MANCC=NHACC.MANCC and PHIEUNHAP.MANV = NHANVIEN.MANV";
                            tb = conn.getTable(sql);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Lỗi: " + e.Message);
                        }
                        break;
                    }
                case 1:
                    {
                        try
                        {
                            String sql = "select MAPN,TENNCC,TENNV,NGAYLAP,TONGTIEN from PHIEUNHAP,NHACC,NHANVIEN where MAPN like '%" + pn + "%' and PHIEUNHAP.MANCC=NHACC.MANCC and PHIEUNHAP.MANV = NHANVIEN.MANV";
                            tb = conn.getTable(sql);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Lỗi: " + e.Message);
                        }
                        break;
                    }
                case 2:
                    {
                        try
                        {
                            String sql = "select MAPN,TENNCC,TENNV,NGAYLAP,TONGTIEN from PHIEUNHAP,NHACC,NHANVIEN where TENNCC like N'%" + ncc + "%' and PHIEUNHAP.MANCC=NHACC.MANCC and PHIEUNHAP.MANV = NHANVIEN.MANV";
                            tb = conn.getTable(sql);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Lỗi: " + e.Message);
                        }
                        break;
                    }
                case 3:
                    {
                        try
                        {
                            string sql = "select *from CHITIET_PN";
                            tb = conn.getTable(sql);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Lỗi: " + e.Message);
                        }
                        break;
                    }
                case 4:
                    {
                        try
                        {
                            string sql = "select *from CHITIET_PN where MAPN like '%"+ctpn+"%'";
                            tb = conn.getTable(sql);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Lỗi: " + e.Message);
                        }
                        break;
                    } 
            }
            return tb;
        }
        //xoa
        public void XoaPN(string mapn)
        {
            try
            {
                string sql1 = "delete CHITIET_PN where MAPN = '" + mapn + "'";
                conn.ExcuteQuerySql(sql1);

                string sql2 = "delete PHIEUNHAP where MAPN = '" + mapn+ "'";
                conn.ExcuteQuerySql(sql2);

                MessageBox.Show("Xóa thành công !!!");
            }
            catch (Exception e)
            {
                MessageBox.Show("Lỗi: " + e.Message);
            }
        }
    }
}
